package org.come.Jpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import org.come.bean.LaborRank;
import org.come.bean.LaborRole;
import org.come.until.AnalysisString;
import org.come.until.CutButtonImage;
import org.come.until.GsonUtil;
import org.come.until.Util;

import com.tool.btn.TrueFeedbackBtn;
import com.tool.tcpimg.UIUtils;



/**
 * @author HGC<br>
 * @time 2020年4月28日 上午10:05:09<br>
 * @class 类名:TrueFeedbackScPanel 五一真情回馈累充、抽奖面板<br>
 */
public class TrueFeedbackScPanel extends JPanel {

    
    private TrueFeedbackBtn btnHomePage,btnUpPage,btnNextPage,btnLastPage,btnAward,BtnGet;

    // 变身卡信息表格
    private JTable tablePankList;
    // 变身卡信息表格中的对象
    private DefaultTableModel tableModel;
    private JLabel labPage;
    public TrueFeedbackScPanel() {
        setPreferredSize(new Dimension(430,450));
        setOpaque(false);
        setLayout(null);
        
        getBtnHomePage();
        getBtnLastPage();
        getBtnNextPage();
        getBtnUpPage();
        getLabPage();
        getBtnAward();
        getBtnGet();
        getTablePankList();
        
    }
    
    /**展示数据*/
    public void showScpane(LaborRank laborRank){
//        tableModel.getDataVector().clear();
        List<LaborRole> laborRoles = laborRank.getRoles();
        maxPage = laborRoles.size()%10 == 0 ? laborRoles.size()/10 :laborRoles.size()/10+1;
        page = 1;
        this.laborRank = laborRank;
        addData(laborRank.getType());
        changeViewImg(laborRank.getType());
    }
    
    public void addData(int type){
        List<LaborRole> laborRoles = laborRank.getRoles();
        int len = laborRoles.size();
        Vector<Vector<String>> dataVector = tableModel.getDataVector();
        int size = dataVector.size();
        int num = (page-1)*10;
        for (int i = 0; i < size; i++) {
            Vector<String> vector = dataVector.get(i);
            if(num<len){
                LaborRole laborRole = laborRoles.get(num);
                vector.set(0, String.valueOf(num+1));
                vector.set(1, laborRole.getName());
                vector.set(2, AnalysisString.lvl(laborRole.getLvl())+"级");
                if(type==0){
                    vector.set(3, String.valueOf(laborRole.getCz()));
                }else if(type == 1){
                    vector.set(3, String.valueOf(laborRole.getCj()));
                }
                num++;
                if(i>=page*9){break;}
            }else{
                vector.set(0, "");
                vector.set(1, "");
                vector.set(2, "");
                vector.set(3, "");
            }
        }
        Vector<String> vector =dataVector.get(10);
        LaborRole role = laborRank.getRole();
        vector.set(0, laborRank.getRank() == 0?"未上榜":String.valueOf(laborRank.getRank()));
        vector.set(1, role.getName());
        vector.set(2, AnalysisString.lvl(role.getLvl())+"级");
        if(type==0){
            vector.set(3, String.valueOf(role.getCz()));
        }else if(type == 1){
            vector.set(3, String.valueOf(role.getCj()));
        }
        labPage.setText(page+"/"+maxPage);
    }
    
    /**切换面板图片*/
    public void changeViewImg(int typeMenu){
        if(typeMenu == 0){
            topImg = CutButtonImage.getImage("inkImg/old/background/S196.png", -1, -1);
            downBackImg = null;
            btnAward.setVisible(true);
            try {
                BtnGet.setBounds(325,93, 80, 26);
                BtnGet.setColors(UIUtils.COLOR_BTNPUTONG);
                BtnGet.setFont(UIUtils.TEXT_HY16);
                BtnGet.setIcons(CutButtonImage.cuts("skin/8100226/button/50.png"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(typeMenu == 1){
            topImg = CutButtonImage.getImage("inkImg/old/background/S197.png", -1, -1);
            downBackImg =CutButtonImage.getImage("inkImg/old/background/S200.png", -1, -1);
            btnAward.setVisible(false);
            try {
                BtnGet.setBounds(359,12, 34, 17);
                BtnGet.setColors(UIUtils.COLOR_BTNTEXT);
                BtnGet.setFont(UIUtils.TEXT_FONT);
                BtnGet.setIcons(CutButtonImage.cuts("skin/8100226/button/2.png"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    private ImageIcon topImg,downImg,downBackImg;
    private LaborRank laborRank;
    private int page,maxPage;
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        if(topImg!=null){
            g.drawImage(topImg.getImage(), 0, 0, null);
        }
        if(downImg==null){
            downImg = CutButtonImage.getImage("inkImg/old/background/S194.png", -1, -1);
        }
        g.drawImage(downImg.getImage(), 0, 131, null);
        if(downBackImg!=null) {
            g.drawImage(downBackImg.getImage(), 0, 131, null);
        }
    }

    public TrueFeedbackBtn getBtnHomePage() {
        if(btnHomePage == null){
            btnHomePage = new TrueFeedbackBtn("skin/8100226/button/2.png", 1, 3, "首页", UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, this);
            btnHomePage.setBounds(124, 416, 34, 17);
            this.add(btnHomePage);
        }
        return btnHomePage;
    }

    public void setBtnHomePage(TrueFeedbackBtn btnHomePage) {
        this.btnHomePage = btnHomePage;
    }

    public TrueFeedbackBtn getBtnUpPage() {
        if(btnUpPage == null){
            btnUpPage = new TrueFeedbackBtn("skin/8100226/button/10.png", 1, 4, this);
            btnUpPage.setBounds(159,416, 18, 18);
            this.add(btnUpPage);
        }
        return btnUpPage;
    }

    public void setBtnUpPage(TrueFeedbackBtn btnUpPage) {
        this.btnUpPage = btnUpPage;
    }

    public TrueFeedbackBtn getBtnNextPage() {
        if(btnNextPage == null){
            btnNextPage = new TrueFeedbackBtn("skin/8100226/button/9.png", 1, 5, this);
            btnNextPage.setBounds(241,416, 18, 18);
            this.add(btnNextPage);
        }
        return btnNextPage;
    }

    public void setBtnNextPage(TrueFeedbackBtn btnNextPage) {
        this.btnNextPage = btnNextPage;
    }

    public TrueFeedbackBtn getBtnLastPage() {
        if(btnLastPage == null){
            btnLastPage = new TrueFeedbackBtn("skin/8100226/button/2.png", 1, 6, "末页", UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, this);
            btnLastPage.setBounds(261, 416, 34, 17);
            this.add(btnLastPage);
        }
        return btnLastPage;
    }

    public void setBtnLastPage(TrueFeedbackBtn btnLastPage) {
        this.btnLastPage = btnLastPage;
    }

    public ImageIcon getTopImg() {
        return topImg;
    }

    public void setTopImg(ImageIcon topImg) {
        this.topImg = topImg;
    }

    public ImageIcon getDownImg() {
        return downImg;
    }

    public void setDownImg(ImageIcon downImg) {
        this.downImg = downImg;
    }
    public JTable getTablePankList() {
        if (tablePankList == null) {
            tablePankList = new JTable() {
                {
                    setOpaque(false);
                }

                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            };

            tablePankList.setShowGrid(false);
            tablePankList.setBounds(1, 152, 426, 288);
            DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {

                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
                        int column) {
                    if (column >= 2) {
                        setForeground(Color.red);
                    } else {
                        setForeground(Color.white);
                    }
                    if(row == 10){
                        setForeground(Color.yellow);
                    }
                    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                }
            };
            renderer.setHorizontalAlignment(SwingConstants.CENTER);
            tablePankList.setDefaultRenderer(Object.class, renderer);

            JTableHeader header = tablePankList.getTableHeader();

            // 设置表头的高度
            header.setPreferredSize(new Dimension(header.getWidth(), 0));
            tablePankList.getTableHeader().setBackground(UIUtils.Color_BACK);
            tablePankList.getTableHeader().setVisible(false);
            tablePankList.getTableHeader().setBorder(BorderFactory.createLineBorder(Color.ORANGE));

            tablePankList.setSelectionBackground(new Color(0, 0, 0, 0)) ;
            tablePankList.setSelectionForeground(Color.white);
            tablePankList.setForeground(Color.white);
            tablePankList.setFont(UIUtils.TEXT_FONT1);
            tablePankList.setBackground(UIUtils.Color_BACK);
            tablePankList.setModel(getTableModel());
            tablePankList.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    int selectedRow = tablePankList.getSelectedRow();
                    if (selectedRow == -1) {
                        return;
                    }
                }
            });
            // 设置列间距428
            tablePankList.getColumnModel().getColumn(0).setPreferredWidth(60);
            tablePankList.getColumnModel().getColumn(1).setPreferredWidth(135);
            tablePankList.getColumnModel().getColumn(2).setPreferredWidth(80);
            tablePankList.getColumnModel().getColumn(3).setPreferredWidth(150);
            // 设置行宽
            tablePankList.setRowHeight(23);
            tablePankList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            tablePankList.isCellEditable(1, 1);
            tablePankList.setEnabled(true);
            this.add(tablePankList);
        }
        return tablePankList;
    }

    public void setTablePankList(JTable tablePankList) {
        this.tablePankList = tablePankList;
    }
    
    public DefaultTableModel getTableModel() {
        if (tableModel == null) {
            tableModel = new DefaultTableModel();

            Vector<Vector<String>> verVectors = new Vector<Vector<String>>();
            for (int i = 0; i < 11; i++) {
                Vector<String> strings = new Vector<>();
                strings.add("");
                strings.add("");
                strings.add("");
                strings.add("");
                verVectors.add(strings);
            }
            tableModel.setDataVector(verVectors, Util.vector3);
        }
        return tableModel;
    }

    public void setTableModel(DefaultTableModel tableModel) {
        this.tableModel = tableModel;
    }

    public JLabel getLabPage() {
        if(labPage == null){
            labPage = new JLabel("1/1",SwingConstants.CENTER);
            labPage.setForeground(Color.white);
            labPage.setFont(UIUtils.TEXT_FONT1);
            labPage.setBounds(226-48, 416, 61, 16);
            this.add(labPage);
        }
        return labPage;
    }

    public void setLabPage(JLabel labPage) {
        this.labPage = labPage;
    }

    public TrueFeedbackBtn getBtnAward() {
        if(btnAward == null){
            btnAward = new TrueFeedbackBtn("skin/8100226/button/49.png", 1, 7, "奖励一览", UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, this);
            btnAward.setBounds(35,102, 68, 17);
            this.add(btnAward);
        }
        return btnAward;
    }

    public void setBtnAward(TrueFeedbackBtn btnAward) {
        this.btnAward = btnAward;
    }

    public TrueFeedbackBtn getBtnGet() {
        if(BtnGet == null){
            BtnGet = new TrueFeedbackBtn("skin/8100226/newbutton/B21.png", 1, 8, "领取", UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY16, this);
            BtnGet.setBounds(325,93, 79, 24);
            this.add(BtnGet);
        }
        return BtnGet;
    }

    public void setBtnGet(TrueFeedbackBtn btnGet) {
        BtnGet = btnGet;
    }
    
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getMaxPage() {
        return maxPage;
    }

    public void setMaxPage(int maxPage) {
        this.maxPage = maxPage;
    }

    public LaborRank getLaborRank() {
        return laborRank;
    }

    public void setLaborRank(LaborRank laborRank) {
        this.laborRank = laborRank;
    }

    
}
